package org.zf.database.factory;

import org.zf.database.consts.DBType;
import org.zf.database.domain.DBInfo;
import org.zf.database.exception.NotSupportException;
import org.zf.database.server.CommonDBServer;
import org.zf.database.server.impl.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取数据库工具实例
 */
public class InstanceFactory
{
    private static final Map<DBType, CommonDBServer> INSTANCES = new HashMap<DBType, CommonDBServer>()
    {{
        put(DBType.MYSQL, new MySQLDBServerImpl());
        put(DBType.ORACLE, new OracleDBServerImpl());
        put(DBType.SQLSERVER, new SQLServerDBServerImpl());
        put(DBType.DB2, new DB2DBServerImpl());
        put(DBType.POSTGRESQL, new PostgreSqlDBServerImpl());
        put(DBType.DAMENG, null);
    }};

    public static CommonDBServer getInstance(DBInfo dbInfo) throws NotSupportException
    {
        CommonDBServer commonDBServer = INSTANCES.get(dbInfo.getDbType());
        if (commonDBServer == null) throw new NotSupportException("不支持的数据库类型");
        return commonDBServer;
    }

    public static CommonDBServer getInstance(String jdbcUrl) throws NotSupportException
    {
        return getInstance(DBType.parseJdbcUrl(jdbcUrl));
    }

    public static CommonDBServer getInstance(DBType dbType) throws NotSupportException
    {
        CommonDBServer commonDBServer = INSTANCES.get(dbType);
        if (commonDBServer == null) throw new NotSupportException("不支持的数据库类型");
        return commonDBServer;
    }
}
